package suspect

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/suspect"
	suspectReq "github.com/flipped-aurora/gin-vue-admin/server/model/suspect/request"
)

type CasSuspectsService struct{}

// CreateCasSuspects 创建casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) CreateCasSuspects(casSuspects *suspect.CasSuspects) (err error) {
	err = global.GVA_DB.Create(casSuspects).Error
	return err
}

// DeleteCasSuspects 删除casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) DeleteCasSuspects(ID string) (err error) {
	err = global.GVA_DB.Delete(&suspect.CasSuspects{}, "id = ?", ID).Error
	return err
}

// DeleteCasSuspectsByIds 批量删除casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) DeleteCasSuspectsByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]suspect.CasSuspects{}, "id in ?", IDs).Error
	return err
}

// UpdateCasSuspects 更新casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) UpdateCasSuspects(casSuspects suspect.CasSuspects) (err error) {
	if casSuspects.CaseId == nil {
		casSuspects.CaseId = new(int)
	}
	err = global.GVA_DB.Model(&suspect.CasSuspects{}).Where("id = ?", casSuspects.ID).Updates(&casSuspects).Error
	return err
}

// GetCasSuspects 根据ID获取casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) GetCasSuspects(ID string) (casSuspects suspect.CasSuspects, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&casSuspects).Error
	return
}

// GetCasSuspectsInfoList 分页获取casSuspects表记录
// Author [yourname](https://github.com/yourname)
func (casSuspectsService *CasSuspectsService) GetCasSuspectsInfoList(info suspectReq.CasSuspectsSearch) (list []suspect.CasSuspects, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&suspect.CasSuspects{})
	var casSuspectss []suspect.CasSuspects
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.CaseId != nil {
		db = db.Where("case_id = ?", *info.CaseId)
	}
	if info.CaseName != nil {
		db = db.Where("case_name like ?", "%"+*info.CaseName+"%")
	}
	if info.Charge != nil && *info.Charge != "" {
		db = db.Where("charge = ?", *info.Charge)
	}
	if info.ApprehendCharge != nil && *info.ApprehendCharge != "" {
		db = db.Where("apprehend_charge = ?", *info.ApprehendCharge)
	}
	if info.Nation != nil && *info.Nation != "" {
		db = db.Where("nation = ?", *info.Nation)
	}
	if info.TownCode != nil && *info.TownCode != "" {
		db = db.Where("town_code = ?", *info.TownCode)
	}
	if info.FOwnerId != nil {
		db = db.Where("f_owner_id = ?", *info.FOwnerId)
	}
	if info.State != nil && *info.State != "" {
		db = db.Where("state = ?", *info.State)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if info.Sort != "" {
		if info.Order == "desc" {
			db = db.Order(info.Sort + " desc")
		} else {
			db = db.Order(info.Sort)
		}
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&casSuspectss).Error
	return casSuspectss, total, err
}
func (casSuspectsService *CasSuspectsService) GetCasSuspectsDataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	caseId := make([]map[string]any, 0)

	global.GVA_DB.Table("cas_cases").Where("deleted_at IS NULL").Select("case_name as label,id as value").Scan(&caseId)
	res["caseId"] = caseId
	fOwnerId := make([]map[string]any, 0)

	global.GVA_DB.Table("sys_users").Where("deleted_at IS NULL").Select("nick_name as label,id as value").Scan(&fOwnerId)
	res["fOwnerId"] = fOwnerId
	return
}
func (casSuspectsService *CasSuspectsService) GetCasSuspectsPublic() {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
